//
//  ZHTQPresentationManager.swift
//  ZHTQWeibo
//
//  Created by ZHTQ on 2018/9/15.
//  Copyright © 2018年 ZHTQ. All rights reserved.
//

import UIKit




class ZHTQPresentationManager: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {

    ///显示菜单试图的尺寸
    var presentedViewFrame = CGRect.zero
    
    ///记录判断当前是现实还是消失
    private var isPresent: Bool = false
    
    
    //MARK:- 内部控制方法
    
    ///菜单试图显示
    private func willforPresented(transitionContext: UIViewControllerContextTransitioning){
        //1.显示的view
        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
            return
        }
        
        //1.1通过transform 来做动画
        toView.transform = CGAffineTransform(scaleX: 1, y: 0)
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        //1.2动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            toView.transform = CGAffineTransform.identity
        }) { (_) in
            transitionContext.completeTransition(true)
        }
        
        //2.将显示的View添加到内容试图中
        transitionContext.containerView.addSubview(toView)
    }
    
    ///菜单试图消失
    private func willforDismissed(transitionContext: UIViewControllerContextTransitioning){
        //拿到将要消失的view
        guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
            return
        }
        //设置transform 动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            
            fromView.transform = CGAffineTransform(scaleX: 1, y: 0.000001)
        }) { (_) in
            //告诉动画完成
            transitionContext.completeTransition(true)
        }
    }
    
    //MARK:- UIViewControllerTransitioningDelegate
    //返回自定义转场控制器
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
    
        let presentation = ZHTQPresentationController(presentedViewController: presented, presenting: presenting)
        presentation.presentedViewFrame = presentedViewFrame
        return presentation
    }
    
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        //present 发送通知给按钮改变状态
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: PresentationPresentedNotification), object: self)
        isPresent = true
        return self
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        //dismiss发送通知给按钮改变状态
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: PresentationDismissNotification), object: self)
        isPresent = false
        return self
    }
    
    //MARK:- UIViewControllerAnimatedTransitioning
    
    //动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.25
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        if isPresent { //显示
            willforPresented(transitionContext: transitionContext)
        } else { //消失
            
            willforDismissed(transitionContext: transitionContext)
        }
    }
    
}
